package ml.kupin.controller;

import ml.kupin.common.utils.KuPinResult;
import ml.kupin.pojo.TbContent;
import ml.kupin.content.service.ContentService;
import ml.kupin.common.vo.EasyUIDataGridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by 小柯(Allen Kerr) on 17.4.27 11:30.
 */
@Controller
@RequestMapping("/content")
public class ContentController {

    @Autowired
    private ContentService contentService;

    /**
     * 获取内容分类对应的内容列表 --> EasyUIDataGridResult
     * 链接：
     * http://localhost:8081/content/query/list?categoryId=89&page=1&rows=20
     */
    @RequestMapping(value = "/query/list", method = RequestMethod.GET)
    @ResponseBody
    public EasyUIDataGridResult<TbContent> contentList(long categoryId, int page, int rows) {
        EasyUIDataGridResult<TbContent> dataGridResult = contentService.getContentsByPage(categoryId, page, rows);
        return dataGridResult;
    }

    /**
     * 向对应的内容分类中插入内容content
     * Request URL:http://localhost:8081/content/save
     * Request Method:POST
     * <p>
     * 页面数据：
     * categoryId、title、subTitle、titleDesc、url、pic、pic2、content
     * <p>
     * 表数据：
     * 内容Id Long id;
     * 内容类目ID Long categoryId
     * 内容标题 String title
     * 子标题 String subTitle
     * 标题描述 String titleDesc
     * 链接 String url
     * 图片绝对路径 String pic
     * 图片2 String pic2
     * Date created
     * Date updated
     * 内容 String content
     * <p>
     * 需要补全：
     * id(自增长), updated, updated
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public KuPinResult addContent(TbContent content) {
        KuPinResult kuPinResult = null;
        try {
            kuPinResult = contentService.addContent(content);
        } catch (Exception e) {
            e.printStackTrace();
            return KuPinResult.notOk("添加失败！");
        }
        return kuPinResult;
    }

    /**
     * 内容删除
     * $.post("/content/delete",{"ids":ids}, function(data){});
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public KuPinResult deleteContent(@RequestParam("ids") long id) {
        return KuPinResult.notOk("方法未实现！");
    }

    /**
     * 内容编辑
     * $.post("/content/edit",$("#contentEditForm").serialize(), function(data){});
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    @ResponseBody
    public KuPinResult editContent(TbContent content) {
        return KuPinResult.notOk("方法未实现！");
    }

}
